package com.puman.watchtrade.fragment.set;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.info.InfoDetailFragment;
import com.puman.watchtrade.fragment.set.httpHandler.SettingHttpHandler;
import com.puman.watchtrade.util.DataResult;
import com.puman.watchtrade.util.Gjfun;
import com.puman.watchtrade.util.LoadingProgressDialog;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint({ "ValidFragment", "NewApi", "ResourceAsColor" })
public class ForgetPassFragment extends Fragment implements OnClickListener {
	private View mView;

	private TextView getCode;
	private TextView inputCode;

	private TextView mobile;
	private TextView nickName;
	private TextView pass;
	private TextView passSure;
	private TextView registerLaw;
	/*
	 * variable
	 */
	private static final int CODE_SUC = 0x1;
	private static final int CODE_FAIL = 0x2;
	public static final int AMITION = 0x3;
	public static final int DELAY_MIL = 1000;
	private static final int RESET_SUC = 0x5;
	private static final int RESET_FAIL = 0x6;
	public int REMAIN_TIME = 60;//倒计时
	
	private DataResult dataResult = new DataResult();
	private SettingHttpHandler settingHttpHandler = new SettingHttpHandler();
	private String type = "1";
	private String recieveCode = "";

	public ForgetPassFragment() {
		super();
	}

	public ForgetPassFragment(String flag) {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.forgetpass_layout, null);
		initView();
		return mView;
	}

	private void initView() {
		mView.findViewById(R.id.main_left_imgbtn).setOnClickListener(this);

		getCode = (TextView) mView.findViewById(R.id.registe_get_code);
		getCode.setOnClickListener(this);

		mobile = (TextView) mView.findViewById(R.id.login_username_input);
		pass = (TextView) mView.findViewById(R.id.login_pass_input);
		passSure = (TextView) mView.findViewById(R.id.login_pass_input_sure);
		registerLaw = (TextView) mView.findViewById(R.id.register_user_law);
		nickName = (TextView) mView.findViewById(R.id.login_nick_input);
		inputCode = (TextView) mView.findViewById(R.id.login_code_input);

		mView.findViewById(R.id.forget_pass).setOnClickListener(this);
		
		

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_left_imgbtn:
			MainActivity.getInstant().fragmentManager.popBackStack();
			break;
		case R.id.register_user_law:
			FragmentTransaction transaction = MainActivity.fragmentManager.beginTransaction();
			AboutFragment aboutFragment = new AboutFragment("1");
			transaction.replace(R.id.main_non_bottom, aboutFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			break;
		case R.id.registe_get_code:
		    if(!Gjfun.isPhoneNumberValid(mobile.getText().toString()))
	        {
	        	Toast.makeText(getActivity(), "手机无效", Toast.LENGTH_LONG).show();
	        	break;
	        }
		    getCode();
			break;
		case R.id.forget_pass:
			if (!checkValidate()) {
				break;
			}
			resetPass();
			break;
		default:
			break;
		}

	}
	/*
	 * 检查密码有效性
	 */
	private boolean checkValidate() {
		if (!inputCode.getText().toString().equals(recieveCode)||recieveCode.equals("")) {
			Toast.makeText(MainActivity.getInstant(), "验证码不正确哦", Toast.LENGTH_SHORT)
			.show();
			inputCode.setText("");
			return false;
		}
		
		if (pass.getText().toString().length() < 6 || pass.getText().toString().length() >18) {
			Toast.makeText(MainActivity.getInstant(), "请输入6-18位密码", Toast.LENGTH_SHORT)
			.show();
			return false;
		}
		
		if (!pass.getText().toString().equals(passSure.getText().toString())) {
			Toast.makeText(MainActivity.getInstant(), "两次密码不一致哦", Toast.LENGTH_SHORT)
			.show();
			return false;
		}
		
		return true;
	}
	/*
	 * 初始化验证码
	 */
	private void initCode() {
		getCode.setBackgroundColor(R.color.grey);
		sendMessageDeLay(AMITION, DELAY_MIL);
	}


	private void getCode() {
		MainActivity.getInstant().cachedThreadPoolMinPriority
				.execute(getCodeRunnable);
	}

	/*
	 * 获取头条各栏目个数
	 */
	Runnable getCodeRunnable = new Runnable() {

		@Override
		public void run() {
			int what = CODE_FAIL;
			dataResult = null;
			dataResult = new DataResult();
			dataResult = settingHttpHandler.valideCode(mobile.getText().toString(), type);
			if (dataResult.flag) {
				what = CODE_SUC;
			}
			sendMessage(what);
		}
	};
	/*
	 * 重置密码
	 */
	private LoadingProgressDialog mProgress = null;
	private void resetPass() {
		Gjfun.startProgressDialog(mProgress, getActivity());
		MainActivity.getInstant().cachedThreadPoolMinPriority
				.execute(resetRunnable);
	}

	/*
	 * 重置密码
	 */
	Runnable resetRunnable = new Runnable() {

		@Override
		public void run() {
			int what = RESET_FAIL;
			dataResult = null;
			dataResult = new DataResult();
			dataResult = settingHttpHandler.resetPass(mobile.getText().toString(), pass.getText().toString());
			if (dataResult.flag) {
				what = RESET_SUC;
			}
			sendMessage(what);
		}
	};

	Callback callback = new Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case CODE_SUC:
				if (REMAIN_TIME == 60) {
					initCode();
				}
				recieveCode = (String) dataResult.object;
				break;
			case CODE_FAIL:
				Toast.makeText(MainActivity.getInstant(), ""+dataResult.msg, Toast.LENGTH_SHORT).show();
				break;
			case RESET_SUC:
				Gjfun.stopProgressDialog(mProgress);
				Toast.makeText(MainActivity.getInstant(), ""+dataResult.msg, Toast.LENGTH_SHORT)
				.show();
				break;
			case RESET_FAIL:
				Gjfun.stopProgressDialog(mProgress);
				Toast.makeText(MainActivity.getInstant(), ""+dataResult.msg, Toast.LENGTH_SHORT)
				.show();
				break;
			case AMITION:
				REMAIN_TIME--;
				getCode.setText(REMAIN_TIME+"s 后重新获取");
				if (REMAIN_TIME != 0) {
					sendMessageDeLay(AMITION, DELAY_MIL);
				}else {
					REMAIN_TIME = 60;
					getCode.setText("重新获取");
					getCode.setTextColor(MainActivity.getInstant().getResources().getColor(R.color.white));
					getCode.setBackgroundColor(R.color.themeColor);
				}
				break;
			default:
				break;
			}
			return false;
		}
	};
	Handler handler = new Handler(callback);

	private void sendMessage(int what) {
		Message msg = new Message();
		msg.what = what;
		handler.sendMessage(msg);
	}
	
	private void sendMessageDeLay(int what, int delay) {
		Message msg = new Message();
		msg.what = what;
		handler.sendMessageDelayed(msg, delay);
	}
	
	/*
	 * 获取头条各栏目个数
	 */
	Runnable CodeRunnable = new Runnable() {

		@Override
		public void run() {
//			String path = "http://115.159.6.217:9595/watchtrade/checkcode.do?randNum=3124";
//			try {
//				URL url = new URL(path); // path图片的网络地址
//				HttpURLConnection httpURLConnection = (HttpURLConnection) url
//						.openConnection();
//				if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
//				 bitmap = BitmapFactory.decodeStream(httpURLConnection
//							.getInputStream());
//					System.out.println("加载网络图片完成");
//				} else {
//					System.out.println("加载网络图片失败");
//				}
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
		}
	
	};

}
